home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 April / PCWorld_2008-04_cd.bin / v cisle / ozo / zotero-1.0.3.xpi / chrome / zotero.jar / content / zotero / addCitationDialog.js < prev    next >
Encoding:
JavaScript  |  2007-12-19  |  14.3 KB  |  492 lines

  1. /*
  2.     ***** BEGIN LICENSE BLOCK *****
  3.     
  4.     Copyright (c) 2006  Center for History and New Media
  5.                         George Mason University, Fairfax, Virginia, USA
  6.                         http://chnm.gmu.edu
  7.     
  8.     Licensed under the Educational Community License, Version 1.0 (the "License");
  9.     you may not use this file except in compliance with the License.
  10.     You may obtain a copy of the License at
  11.     
  12.     http://www.opensource.org/licenses/ecl1.php
  13.     
  14.     Unless required by applicable law or agreed to in writing, software
  15.     distributed under the License is distributed on an "AS IS" BASIS,
  16.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.     See the License for the specific language governing permissions and
  18.     limitations under the License.
  19.     
  20.     ***** END LICENSE BLOCK *****
  21. */
  22.  
  23. var Zotero_Citation_Dialog = new function () {
  24.     var _preserveData = {
  25.         "prefix":"value",
  26.         "suffix":"value",
  27.         "locatorType":"selectedIndex",
  28.         "locator":"value",
  29.         "suppressAuthor":"checked"
  30.     };
  31.     
  32.     var _itemData = new Object();
  33.     var _multipleSourcesOn = false;
  34.     var _lastSelected = null;
  35.     var _previewShown = false;
  36.     var _suppressNextTreeSelect = false;
  37.     var _locatorIndexArray = {};
  38.     var _autoRegeneratePref;
  39.     var _acceptButton;
  40.     var _sortCheckbox;
  41.     var _originalHTML;
  42.     var io;
  43.     
  44.     this.load = load;
  45.     this.toggleMultipleSources = toggleMultipleSources;
  46.     this.toggleEditor = toggleEditor;
  47.     this.treeItemSelected = treeItemSelected;
  48.     this.listItemSelected = listItemSelected;
  49.     this.add = add;
  50.     this.remove = remove;
  51.     this.sortCitation = sortCitation;
  52.     this.confirmRegenerate = confirmRegenerate;
  53.     this.accept = accept;
  54.     this.cancel = cancel;
  55.     
  56.     /*
  57.      * initialize add citation dialog
  58.      */
  59.     function load() {
  60.         document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");
  61.         document.getElementById("show-editor-button").label = Zotero.getString("citation.showEditor");
  62.         
  63.         if(Zotero.isWin) {
  64.             document.getElementById("zotero-select-items-container").style.border = "1px solid black";
  65.         }
  66.         io = window.arguments[0].wrappedJSObject;
  67.         
  68.         // find accept button
  69.         _acceptButton = document.getElementById("add-citation-dialog").getButton("accept");
  70.         _autoRegeneratePref = Zotero.Prefs.get("integration.autoRegenerate");
  71.         
  72.         // if a style with sortable citations, present checkbox
  73.         if(io.citation.sortable) {
  74.             _sortCheckbox = document.getElementById("keepSorted");
  75.             _sortCheckbox.hidden = false;
  76.             _sortCheckbox.checked = true;
  77.             io.citation.properties.sort = true;
  78.         }
  79.         
  80.         // load locators
  81.         var locators = Zotero.CSL.Global.getLocatorStrings();
  82.         var menu = document.getElementById("locatorType");
  83.         var popup = document.getElementById("locator-type-popup");
  84.         var i = 0;
  85.         for(var value in locators) {
  86.             var locator = locators[value];
  87.             locator = locator[0].toUpperCase()+locator.substr(1);
  88.             // add to popup
  89.             var child = document.createElement("menuitem");
  90.             child.setAttribute("value", value);
  91.             child.setAttribute("label", locator);
  92.             popup.appendChild(child);
  93.             // add to array
  94.             _locatorIndexArray[value] = i;
  95.             i++;
  96.         }
  97.         menu.selectedIndex = 0;
  98.         
  99.         // load (from selectItemsDialog.js)
  100.         doLoad();
  101.         
  102.         // if we already have a citation, load data from it
  103.         document.getElementById('editor').format = "Integration";
  104.         if(io.citation.citationItems.length) {
  105.             if(io.citation.citationItems.length == 1) {
  106.                 // single citation
  107.                 _suppressNextTreeSelect = true;
  108.                 itemsView.selectItem(io.citation.citationItems[0].itemID);     // treeview from selectItemsDialog.js
  109.                 for(var property in _preserveData) {
  110.                     if(io.citation.citationItems[0][property]) {
  111.                         if(property == "locatorType") {
  112.                             document.getElementById(property)[_preserveData[property]] = _locatorIndexArray[io.citation.citationItems[0][property]];
  113.                         } else {
  114.                             document.getElementById(property)[_preserveData[property]] = io.citation.citationItems[0][property];
  115.                         }
  116.                     }
  117.                 }
  118.             } else {
  119.                 // multiple citations
  120.                 toggleMultipleSources();
  121.                 for(var i=0; i<io.citation.citationItems.length; i++) {
  122.                     var item = Zotero.Items.get(io.citation.citationItems[i].itemID);
  123.                     if(item) {
  124.                         _addItem(item);
  125.                         _itemData[io.citation.citationItems[i].itemID] = io.citation.citationItems[i];
  126.                     }
  127.                 }
  128.             }
  129.             
  130.             // show user-editable edited citation
  131.             if(io.citation.properties.custom) {
  132.                 toggleEditor(io.citation.properties.custom);
  133.                 io.citation.properties.custom = undefined;
  134.             }
  135.             
  136.             _updateAccept();
  137.         }
  138.     }
  139.     
  140.     /*
  141.      * turn on/off multiple sources item list
  142.      */
  143.     function toggleMultipleSources() {
  144.         _multipleSourcesOn = !_multipleSourcesOn;
  145.         if(_multipleSourcesOn) {
  146.             document.getElementById("multiple-sources").hidden = undefined;
  147.             document.getElementById("add-citation-dialog").width = "750";
  148.             document.getElementById("multiple-sources-button").label = Zotero.getString("citation.singleSource");            
  149.             window.sizeToContent();
  150.             window.moveTo((window.screenX-75), window.screenY);
  151.             treeItemSelected();
  152.             // disable adding info until citation added
  153.             _itemSelected(false);
  154.         } else {
  155.             document.getElementById("multiple-sources").hidden = true;
  156.             document.getElementById("add-citation-dialog").width = "600";
  157.             document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");            
  158.             window.sizeToContent();
  159.             window.moveTo((window.screenX+75), window.screenY);
  160.             
  161.             // enable all fields
  162.             for(var i in _preserveData) {
  163.                 document.getElementById(i).disabled = false;
  164.             }
  165.             
  166.             // delete item list
  167.             _itemData = new Object();
  168.             
  169.             // delete all items
  170.             _clearCitationList();
  171.         }
  172.         _updateAccept();
  173.         _updatePreview();
  174.     }
  175.     
  176.     /*
  177.      * called when an item in the item selection tree is clicked
  178.      */
  179.     function treeItemSelected() {
  180.         if(_suppressNextTreeSelect) {
  181.             _suppressNextTreeSelect = false;
  182.             _updateAccept();
  183.             return;
  184.         }
  185.         var items = itemsView.getSelectedItems(true); // treeview from selectItemsDialog.js
  186.         var itemID = (items.length ? items[0] : false);
  187.         
  188.         if(_multipleSourcesOn) {
  189.             // if item is also on right side, show info
  190.             var hasBeenAdded = itemID && _itemData[itemID] !== undefined;
  191.             // disable boxes if item not added; otherwise, enable
  192.             _itemSelected(hasBeenAdded ? itemID : false);
  193.             // disable adding nothing, or things already added
  194.             document.getElementById("add").disabled = !itemID || hasBeenAdded;
  195.         } else {
  196.             _updateAccept();
  197.             _updatePreview();
  198.         }
  199.     }
  200.     
  201.     /*
  202.      * called when an item in the selected items list is clicked
  203.      */
  204.     function listItemSelected() {
  205.         var selectedListItem = document.getElementById("citation-list").getSelectedItem(0);
  206.         var itemID = (selectedListItem ? selectedListItem.value : false);
  207.         _itemSelected(itemID);
  208.         
  209.         document.getElementById("remove").disabled = !itemID;
  210.     }
  211.     
  212.     /*
  213.      * Adds a citation to the multipleSources list
  214.      */
  215.     function add() {
  216.         var item = itemsView.getSelectedItems()[0]; // treeview from selectItemsDialog.js
  217.         _itemSelected(item.getID());
  218.         _addItem(item);
  219.         
  220.         // don't let someone select it again
  221.         document.getElementById("add").disabled = true;
  222.         
  223.         // allow user to press OK
  224.         _updateAccept();
  225.         _updatePreview();
  226.         sortCitation();
  227.     }
  228.     
  229.     /*
  230.      * Deletes a citation from the multipleSources list
  231.      */
  232.     function remove() {
  233.         var citationList = document.getElementById("citation-list");
  234.         var selectedListItem = citationList.getSelectedItem(0);
  235.         var itemID = selectedListItem.value;
  236.         
  237.         // remove from _itemData
  238.         delete _itemData[itemID];
  239.         _itemData[itemID] = undefined;
  240.         _lastSelected = null;
  241.         
  242.         // re-select currently selected in left pane
  243.         var itemIDs = itemsView.getSelectedItems(true); // treeview from selectItemsDialog.js
  244.         if(itemIDs.length) {
  245.             document.getElementById("zotero-items-tree").focus();
  246.             treeItemSelected();
  247.         }
  248.         
  249.         // remove from list
  250.         citationList.removeChild(selectedListItem);
  251.         
  252.         _updateAccept();
  253.         _updatePreview();
  254.         treeItemSelected();
  255.     }
  256.     
  257.     /*
  258.      * Sorts the list of citations
  259.      */
  260.     function sortCitation() {
  261.         io.citation.properties.sort = _sortCheckbox.checked;
  262.         if(_sortCheckbox.checked) {
  263.             _getCitation();
  264.             
  265.             // delete all existing items from list
  266.             _clearCitationList();
  267.             
  268.             // run preview function to re-sort, if it hasn't already been
  269.             // run
  270.             io.previewFunction();
  271.             
  272.             // add items back to list
  273.             for(var i=0; i<io.citation.citationItems.length; i++) {
  274.                 var item = Zotero.Items.get(io.citation.citationItems[i].itemID);
  275.                 _addItem(item);
  276.             }
  277.         }
  278.     }
  279.     
  280.     /*
  281.      * Ask whether to modfiy the preview
  282.      */
  283.     function confirmRegenerate(focusShifted) {
  284.         if(document.getElementById('editor').value == _originalHTML) {
  285.             // no changes; just update without asking
  286.             _updatePreview();
  287.             return;
  288.         }
  289.         
  290.         if(_autoRegeneratePref == -1) {
  291.             if(focusShifted) {    // only ask after onchange event; oninput is too
  292.                                 // frequent for this to be worthwhile
  293.                 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  294.                                                 .getService(Components.interfaces.nsIPromptService);
  295.                 
  296.                 var saveBehavior = { value: false };
  297.                 var regenerate = promptService.confirmEx(
  298.                     this.window,
  299.                     Zotero.getString('integration.regenerate.title'),
  300.                     Zotero.getString('integration.regenerate.body'),
  301.                     promptService.STD_YES_NO_BUTTONS,
  302.                     null, null, null,
  303.                     Zotero.getString('integration.regenerate.saveBehavior'),
  304.                     saveBehavior
  305.                 );
  306.                 
  307.                 if(saveBehavior.value) {
  308.                     _autoRegeneratePref = (regenerate == 0 ? 1 : 0);
  309.                     Zotero.Prefs.set("integration.autoRegenerate", _autoRegeneratePref);
  310.                 }
  311.                 
  312.                 if(regenerate == 0) {
  313.                     _updatePreview();
  314.                 }
  315.             }
  316.         } else if(_autoRegeneratePref == 1) {
  317.             _updatePreview();
  318.         }
  319.     }
  320.     
  321.     /*
  322.      * Shows the edit pane
  323.      */
  324.     function toggleEditor(text) {
  325.         var editor = document.getElementById('editor');
  326.         editor.hidden = _previewShown;
  327.         _previewShown = !_previewShown;
  328.         
  329.         if(_previewShown) {
  330.             document.getElementById("show-editor-button").label = Zotero.getString("citation.hideEditor");        
  331.             window.sizeToContent();
  332.             if(text) {
  333.                 editor.value = text;
  334.             } else {
  335.                 _updatePreview();
  336.             }
  337.         } else {
  338.             document.getElementById("show-editor-button").label = Zotero.getString("citation.showEditor");        
  339.             window.sizeToContent();
  340.         }
  341.     }
  342.     
  343.     /*
  344.      * called when accept button is clicked
  345.      */
  346.     function accept() {
  347.         _getCitation();
  348.         if(_previewShown && io.citation.citationItems.length    // if a citation is selected
  349.                 && document.getElementById('editor').value != _originalHTML) {    // and citation has been edited
  350.             io.citation.properties.custom = document.getElementById('editor').value;
  351.         }
  352.     }
  353.     
  354.     /*
  355.      * called when cancel button is clicked
  356.      */
  357.     function cancel() {
  358.         io.citation.citationItems = new Array();
  359.     }
  360.     
  361.     /*
  362.      * Updates the contents of the preview pane
  363.      */
  364.     function _updatePreview() {
  365.         if(_previewShown) {
  366.             var editor = document.getElementById('editor');
  367.             _getCitation();
  368.             
  369.             editor.readonly = !io.citation.citationItems.length;
  370.             editor.value = _originalHTML = (io.citation.citationItems.length ? io.previewFunction() : "");
  371.         }
  372.     }
  373.     
  374.     /*
  375.      * Controls whether the accept (OK) button should be enabled
  376.      */
  377.     function _updateAccept(status) {
  378.         if(_multipleSourcesOn) {
  379.             _acceptButton.disabled = !document.getElementById("citation-list").childNodes.length;
  380.         } else {
  381.             _acceptButton.disabled = !itemsView.getSelectedItems().length; // treeview from selectItemsDialog.js
  382.         }
  383.     }
  384.     
  385.     /*
  386.      * called when an item is selected; if itemID is false, disables fields; if
  387.      * itemID is undefined, only updates _itemData array
  388.      */
  389.     function _itemSelected(itemID) {
  390.         if(_lastSelected && !_itemData[_lastSelected]) {
  391.             _itemData[_lastSelected] = new Object();
  392.         }
  393.         
  394.         for(var box in _preserveData) {
  395.             var domBox = document.getElementById(box);
  396.             var property = _preserveData[box];
  397.             
  398.             // save property
  399.             if(_lastSelected) {
  400.                 if(property == "locatorType") {
  401.                     _itemData[_lastSelected][box] = domBox.selectedItem.value;
  402.                 } else {
  403.                     _itemData[_lastSelected][box] = domBox[property];
  404.                 }
  405.             }
  406.             // restore previous property
  407.             if(itemID) {
  408.                 domBox.disabled = false;
  409.                 if(_itemData[itemID] && _itemData[itemID][box] !== undefined) {
  410.                     if(property == "locatorType") {
  411.                         domBox[property] = _locatorIndexArray[_itemData[itemID][box]];
  412.                     } else {
  413.                         domBox[property] = _itemData[itemID][box];
  414.                     }
  415.                 }
  416.             } else if(itemID !== undefined) {
  417.                 domBox.disabled = true;
  418.                 domBox[property] = "";
  419.             }
  420.         }
  421.         
  422.         if(itemID !== undefined) _lastSelected = itemID;
  423.     }
  424.     
  425.     /*
  426.      * updates io.citation to reflect selected items
  427.      */
  428.     function _getCitation() {
  429.         io.citation.citationItems = new Array();
  430.         
  431.         // use to map selectedIndexes back to page/paragraph/line
  432.         var locatorTypeElements = document.getElementById("locatorType").getElementsByTagName("menuitem");
  433.         if(_multipleSourcesOn) {
  434.             _itemSelected();        // store locator info
  435.             
  436.             var citationList = document.getElementById("citation-list");
  437.             var listLength = citationList.childNodes.length;
  438.             
  439.             var citationItems = new Array();
  440.             if(listLength) {
  441.                 // generate citationItems
  442.                 for(var i=0; i<listLength; i++) {
  443.                     var itemID = citationList.childNodes[i].value;
  444.                     
  445.                     var citationItem = _itemData[itemID];
  446.                     citationItem.itemID = itemID;
  447.                     io.citation.citationItems.push(citationItem);
  448.                 }
  449.             }
  450.         } else {
  451.             var items = itemsView.getSelectedItems(true); // treeview from selectItemsDialog.js
  452.             
  453.             var citationItem = new Zotero.CSL.CitationItem();
  454.             citationItem.itemID = items[0];
  455.             for(var property in _preserveData) {
  456.                 if(property == "locatorType") {
  457.                     citationItem[property] = document.getElementById(property).selectedItem.value;
  458.                 } else {
  459.                     citationItem[property] = document.getElementById(property)[_preserveData[property]];
  460.                 }
  461.             }
  462.             
  463.             if(citationItem["locator"] == "") {
  464.                 citationItem["locator"] = citationItem["locatorType"] = undefined;
  465.             }
  466.             
  467.             io.citation.citationItems = [citationItem];
  468.         }
  469.     }
  470.     
  471.     /*
  472.      * Add an item to the item list (multiple sources only)
  473.      */
  474.     function _addItem(item) {
  475.         var itemNode = document.createElement("listitem");
  476.         itemNode.setAttribute("value", item.getID());
  477.         itemNode.setAttribute("label", item.getField("title"));
  478.         itemNode.setAttribute("class", "listitem-iconic");
  479.         itemNode.setAttribute("image", item.getImageSrc());
  480.         document.getElementById("citation-list").appendChild(itemNode);
  481.     }
  482.     
  483.     /*
  484.      * Removes all items from the multiple sources list
  485.      */
  486.     function _clearCitationList() {
  487.         var citationList = document.getElementById("citation-list");
  488.         while(citationList.firstChild) {
  489.             citationList.removeChild(citationList.firstChild);
  490.         }
  491.     }
  492. }